package handler;

import java.util.Arrays;
import java.util.List;
import java.util.Observable;


public class Handler extends Observable{

	final protected DeformationPoint[] basePoints;
	final protected DeformationPoint[] movedPoints;
	final protected List<DeformationPoint> basePointList;
	final protected List<DeformationPoint> movedPointList;
	private int deformationStatus =1;
	public Handler(DeformationPoint[] basePoints) {
		super();
		this.basePoints = Arrays.copyOf(basePoints, basePoints.length);
		this.movedPoints = Arrays.copyOf(basePoints, basePoints.length);
		this.basePointList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(this.basePoints));
		this.movedPointList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(this.movedPoints));
		
	}
	
	
	public Handler(DeformationPoint[] basePoints, DeformationPoint[] movedPoints) {
		super();
		this.basePoints = Arrays.copyOf(basePoints, basePoints.length);
		this.movedPoints = Arrays.copyOf(movedPoints, movedPoints.length);
		this.basePointList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(this.basePoints));
		this.movedPointList = java.util.Collections.unmodifiableList(java.util.Arrays.asList(this.movedPoints));
		
	}


	public int size(){
		return basePoints.length;
	}
	
	
	/**
	 * @return the copy of basePoints
	 */
	public DeformationPoint[] getBasePointsArray() {
		return Arrays.copyOf(basePoints, basePoints.length);
	}


	/**
	 * @return the copy of movedPoints
	 */
	public DeformationPoint[] getMovedPointsArray() {
		return Arrays.copyOf(movedPoints, movedPoints.length);
	}


	public final synchronized void elementalMove(int i, DeformationPoint p){
		movedPoints[i] = p;
		deformationStatus++;
		super.setChanged();
		super.notifyObservers(Integer.valueOf(i));
	}
	
	
	
	public final synchronized boolean elementalMoveAll(DeformationPoint[] points){
		if (this.movedPoints.length != points.length) {
			return false;
		}
		for (int i = 0; i < this.movedPoints.length; i++) {
			this.movedPoints[i] = points[i];
		}
		deformationStatus++;
		super.setChanged();
		super.notifyObservers();
		return true;
	}


	public int getDeformationStatus() {
		return deformationStatus;
	}




}
